<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all settings of blog.
  Author: Yashpal
  Usage:  	$this->load->model('Settings_model','',TRUE);
			$this->Settings_model->Blog_Settings();  - get settings value from database
			
			

  Function name : 
  Blog_Settings   -> This function will give the blog settings values.
 

*/


class Settings_model extends Model {

		
    function Settings_model()

    {
        // Call the Model constructor
        parent::Model();
    }
	
	function GetGeneralSetting()
	{
			$query = $this->db->get('generalsettings');
			$generalsettings = $query->row_array();

			return $generalsettings; 	
	}

	function GetAdvancedSetting()
	{
			$query = $this->db->get('advancedsettings');
			$advancedsettings = $query->row_array();
			//print_r($advancedsettings[0]);
			return $advancedsettings; 	
	}


	function SaveGeneralSettings()
	{
		
		$post['BLOGName'] = $this->input->post('BLOGName');
		$post['BLOGTitle'] = $this->input->post('BLOGTitle');	
		$post['BLOGMaintainedBy'] = $this->input->post('BLOGMaintainedBy');	
		$post['UploadFileSize']  = $this->input->post('UploadFileSize');	

		if($this->input->post('GuestView'))
			$post['GuestView'] = 1;
		else
			$post['GuestView'] = 0;	
		
		if($this->input->post('EnableMORE'))
			$post['EnableMORE'] = 1;
		else
			$post['EnableMORE'] = 0;
			
		if($this->input->post('ShowRecentComments'))
			$post['ShowRecentComments'] = 1;
		else
			$post['ShowRecentComments'] = 0;
		
		if($this->input->post('ShowRecentPosts'))
			$post['ShowRecentPosts'] = 1;
		else
			$post['ShowRecentPosts'] = 0;
			
		if($this->input->post('ShowCalendar'))
			$post['ShowCalendar'] = 1;
		else
			$post['ShowCalendar'] = 0;
			
		if($this->input->post('ShowOnlineMembers'))
			$post['ShowOnlineMembers'] = 1;
		else
			$post['ShowOnlineMembers'] = 0;
			
		if($this->input->post('ShowOnlineGuests'))
			$post['ShowOnlineGuests'] = 1;
		else
			$post['ShowOnlineGuests'] = 0;
			
		if($this->input->post('ShowOnlineMemberNames'))
			$post['ShowOnlineMemberNames'] = 1;
		else
			$post['ShowOnlineMemberNames'] = 0;
			
		if($this->input->post('EnableCommentNotification'))
			$post['EnableCommentNotification'] = 1;
		else
			$post['EnableCommentNotification'] = 0;
			
		if($this->input->post('EnablePostScheduling'))
			$post['EnablePostScheduling'] = 1;
		else
			$post['EnablePostScheduling'] = 0;
			
		if($this->input->post('EnableUploadingFiles'))
			$post['EnableUploadingFiles'] = 1;
		else
			$post['EnableUploadingFiles'] = 0;
			
		if($this->input->post('EnableComments'))
			$post['EnableComments'] = 1;
		else
			$post['EnableComments'] = 0;
			
		if($this->input->post('EnablePlugins'))
			$post['EnablePlugins'] = 1;
		else
			$post['EnablePlugins'] = 0;
			
		if($this->input->post('EnableRegistration'))
			$post['EnableRegistration'] = 1;
		else
			$post['EnableRegistration'] = 0;
			
		if($this->input->post('EnableGuestComment'))
			$post['EnableGuestComment'] = 1;
		else
			$post['EnableGuestComment'] = 0;
			
		if($this->input->post('SendRegistrationMessage'))
			$post['SendRegistrationMessage'] = 1;
		else
			$post['SendRegistrationMessage'] = 0;	
		
		//print_r($post);
		$this->db->where('GSID','1');
		$result=$this->db->update('generalsettings', $post);
		
		if($result)
			$data['info']="Settings successfully saved.";
		else
			$data['info']="Settings failed to update";	
		
		
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/GeneralSettings/';
			$data['time']="1";
			
			self::reload_settings();
			$this->load->view('admin/info',$data);
		
		//return $result;
	}

  //function
  
  //********************	
	function SaveAdvancedSettings()
	{
		
		
		$post['NewUserDefaultRole'] = $this->input->post('NewUserDefaultRole');
		
		if($this->input->post('ShowCategories'))
			$post['ShowCategories'] = 1;
		else
			$post['ShowCategories'] = 0;	
			
		if($this->input->post('ShowArchives'))
			$post['ShowArchives'] = 1;
		else
			$post['ShowArchives'] = 0;	
		
		/*if($this->input->post('ShowCommentsShort'))
			$post['ShowCommentsShort'] = 1;
		else
			$post['ShowCommentsShort'] = 0;	
		
		if($this->input->post('ShowAuthorName'))
			$post['ShowAuthorName'] = 1;
		else
			$post['ShowAuthorName'] = 0;	
			
		if($this->input->post('ShowDate'))
			$post['ShowDate'] = 1;
		else
			$post['ShowDate'] = 0;	
			
		if($this->input->post('ShowPermanentLink'))
			$post['ShowPermanentLink'] = 1;
		else
			$post['ShowPermanentLink'] = 0;	
			
		if($this->input->post('ShowSocialSiteLinks'))
			$post['ShowSocialSiteLinks'] = 1;
		else
			$post['ShowSocialSiteLinks'] = 0;	
		
		if($this->input->post('ShowPostedIn'))
			$post['ShowPostedIn'] = 1;
		else
			$post['ShowPostedIn'] = 0;				
	
  		if($this->input->post('ShowAuthorPic'))
			$post['ShowAuthorPic'] = 1;
		else
			$post['ShowAuthorPic'] = 0;				
		
		if($this->input->post('ShowMeta'))
			$post['ShowMeta'] = 1;
		else
			$post['ShowMeta'] = 0;		
			
		if($this->input->post('RemoveCopyright'))
			$post['RemoveCopyright'] = 1;
		else
			$post['RemoveCopyright'] = 0;		
		
		if($this->input->post('EnableTemplateEditing'))
			$post['EnableTemplateEditing'] = 1;
		else
			$post['EnableTemplateEditing'] = 0;		

		if($this->input->post('SendMailErrorMessage'))
			$post['SendMailErrorMessage'] = 1;
		else
			$post['SendMailErrorMessage'] = 0;	
		
		if($this->input->post('ShowZeroPostMonths'))
			$post['ShowZeroPostMonths'] = 1;
		else
			$post['ShowZeroPostMonths'] = 0;	*/
			
		if($this->input->post('SendMailToNewUsers'))
			$post['SendMailToNewUsers'] = 1;
		else
			$post['SendMailToNewUsers'] = 0;		
	
		
		if($this->input->post('SendRegistrationTopic'))
			$post['SendRegistrationTopic'] = 1;
		else
			$post['SendRegistrationTopic'] = 0;	

		$post['NumberOfPostsHome'] = $this->input->post('NumberOfPostsHome');
				
		if($this->input->post('SendRegistrationTopic'))
			$post['SendRegistrationTopic'] = 1;
		else
			$post['SendRegistrationTopic'] = 0;	
			

		$post['NumberofPostsArchives'] =$this->input->post('NumberofPostsArchives');

		$this->db->where('ASID','1');
		$result=$this->db->update('advancedsettings', $post);
		
		if($result)
			$data['info']="Settings successfully saved.";
		else
			$data['info']="Settings failed to update";	
		
		
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/AdvancedSettings/';
			$data['time']="1";
			self::reload_settings();
			$this->load->view('admin/info',$data);
		
	}
	
	//this will save skin name and folder name into database for theming system.
	
	/*user can't change skin.To view skin they need to logout and login again.
	or open site in new broswer. or same broswer close it and open again. 
	(closing n opening won't work in firefox ....keep last session as it is.
	In firefox logout and see New theme is ready.) 
	changes reflect for current admin only.
	*/
		
	//I'm working to provide good solution for this.hang on...	
	function SaveThemeSettings($theme_folder,$theme_name)
	{
		
		
		$post['ThemeName'] = $theme_name;	
		$post['ThemeFolder'] = $theme_folder;	
		
		$this->db->where('GSID','1');
		$result=$this->db->update('generalsettings', $post);
		
		if($result)
			$data['info']="Theme Settings successfully saved.";
		else
			$data['info']="Theme Settings failed to update";	
		
		
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/Themes/';
			$data['time']="1";
			self::reload_settings();
			$this->load->view('admin/info',$data);
	
	}
	
	/*execute in constructor of blog and save all settings into session.
	Each time it runs but it will create session only once.*/
	function BlogSettings()
	{
	
		if($this->session->userdata('SettingsDone')!='1')
		{			
			$query = $this->db->get('generalsettings');
			$mconfig = $query->row_array();
			$query = $this->db->get('advancedsettings');
			$adconfig = $query->row_array();
			$mconfig=array_merge($mconfig,$adconfig);
			$mconfig['SettingsDone']='1';
			
			$this->session->set_userdata($mconfig);
			
		}
		

	} //end of BlogSettings func
	
	function reload_settings()
	{
	
					
			$query = $this->db->get('generalsettings');
			$mconfig = $query->row_array();
			$query = $this->db->get('advancedsettings');
			$adconfig = $query->row_array();
			$mconfig=array_merge($mconfig,$adconfig);
			$mconfig['SettingsDone']='1';
			
			$this->session->unset_userdata($mconfig);
			
		

	} //end of reload_settings func
	
} //end of class
	
